<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>滴答办公系统-学员更新</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="media/layui/css/layui.css" media="all">
<script type="text/javascript" src="media/js/jquery.min.js"></script>
</head>
<body>
	<div class="layui-container" style="margin-top: 5px">
		<form class="layui-form" action="studentadd.do" method="post">
			<div class="layui-form-item">
				<label class="layui-form-label">学员学号</label>
				<div class="layui-input-block">
					<input type="text" name="no" lay-verify="name" autocomplete="off"
						placeholder="请输入学号" id="no" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">学员姓名</label>
				<div class="layui-input-block">
					<input type="text" name="name" id="name" lay-verify="name" autocomplete="off" placeholder="请输入姓名" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">所属班级</label>
				<div class="layui-input-block">
					<select name="gid" id="cds">
						<option value="-1">--请选择班级--</option>
					</select>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">性别</label>
				<div class="layui-input-block">
					<input type="radio" name="sex" value="男" title="男">
					<input type="radio" name="sex" value="女" title="女">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">邮箱</label>
				<div class="layui-input-inline">
					<input type="text" name="email" id="email" lay-verify="required" placeholder="请输入有效" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">手机号</label>
				<div class="layui-input-inline">
					<input type="text" name="phone" id="phone" lay-verify="required" placeholder="请输入手机号" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">QQ</label>
				<div class="layui-input-inline">
					<input type="text" name="qq" id="qq" lay-verify="required" placeholder="请输入QQ" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">身份证号</label>
				<div class="layui-input-inline">
					<input type="text" name="cardno" id="cardno" lay-verify="required" placeholder="请输入身份证号" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">毕业学校</label>
				<div class="layui-input-inline">
					<input type="text" name="school" id="school" lay-verify="required" placeholder="请输入毕业学校" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">学历</label>
				<div class="layui-input-inline">
					<select name="education" id="education">
						<option value="-1">--请选择学历--</option>
						<option value="初中">初中</option>
						<option value="高中">高中</option>
						<option value="专科">专科</option>
						<option value="本科">本科</option>
						<option value="硕士">硕士</option>
						<option value="博士">博士</option>
					</select>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">出生日期</label>
				<div class="layui-input-inline">
					<input type="text" name="birthday" id="date1" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">入学日期</label>
				<div class="layui-input-inline">
					<input type="text" name="createdate" id="date2" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<input class="layui-btn"  style="margin-left: 10%" id="btn1" onclick="modStudent()" value="确认更新">
			</div>
		</form>
	</div>


	<script src="media/layui/layui.js"></script>
	<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
	<script>
		layui.use(['jquery','form','upload', 'layedit', 'laydate'], function() {
      var $ = layui.$, form = layui.form, layer = layui.layer, layedit = layui.layedit, laydate = layui.laydate, upload = layui.upload;
      //日期
      laydate.render({
        elem : '#date1'
      });
      laydate.render({
        elem : '#date2'
      });
      
      function init() {
        $.ajax({
          url: "/emp/student/getStudent",
          type: "POST",
          dataType: "json",
          data: {
            id: getQueryVariable("id")
          }, //提交表单的数据
          success: function(res) {
            analyseData(res);
          },
          error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest.status);
            alert(XMLHttpRequest.readyState);
            alert(textStatus);
          }
        });
      }
      
      init();
      
      function analyseData(data) {
        var no = data.student.no;
        var name = data.student.name;
        var class_id = data.student.class_id;
        var sex = data.student.sex;
        var email = data.student.email;
        var phone = data.student.phone;
        var qq = data.student.qq;
        var cardno = data.student.cardno;
        var school = data.student.school;
        var education = data.student.education;
        var birthday = data.student.birthday;
        var createdate = data.student.createdate;
        
        console.log(qq);
        
        $("#no").val(no);
        $("#name").val(name);
        $("input[name='sex'][value='" + sex + "']").attr("checked",true);
        $("#email").val(email);
        $("#phone").val(phone);
        $("#qq").val(qq);
        $("#cardno").val(cardno);
        $("#school").val(school);
        $("select[name='education']").val(education);
        $("#date1").val(birthday);
        $("#date2").val(createdate);
        
        getClass(class_id);
      }
      
      function getClass(class_id) {
        $.ajax({
          url: "/emp/classes/getAllClasses",
          type: "POST",
          dataType: "json",
          data: {}, //提交表单的数据
          success: function(res) {
           var options = "<option value=\"-1\">--请选择班级--</option>";
           for(var i = 0; i < res.classesLists.length; i++) {
             if(class_id == res.classesLists[i].id) {
               options += "<option value='" + res.classesLists[i].id + "' selected=\"selected\">" + res.classesLists[i].class_name + "</option>";
             }else{
               options += "<option value='" + res.classesLists[i].id + "'>" + res.classesLists[i].class_name + "</option>";
             }
           }
           // console.log(options);
           $('#cds').empty();
           $("#cds").append(options);
           
           form.render();  
          },
          error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest.status);
            alert(XMLHttpRequest.readyState);
            alert(textStatus);
          }
        });
      }
		});
    
    function getQueryVariable(variable) {
      var query = window.location.search.substring(1);
      var vars = query.split("&");
      for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
          return pair[1];
        }
      }
      return (false);
    }
    
    function modStudent() {
      var no = $('#no').val();
      var name = $('#name').val();
      var sex = $('input[name="sex"]:checked').val();
      var cardno = $('#cardno').val();
      var birthday = $('#date1').val();
      var school = $('#school').val();
      var education = $('#education').val();
      var class_id = $('#cds').val();
      var email = $('#email').val();
      var qq = $('#qq').val();
      var phone = $('#phone').val();
      var createdate = $('#date2').val();
      
      if(no != "" && name != "" && cardno != "" && birthday != "" && school != "" && education != -1 && class_id != -1 && email != "" && qq != "" && phone != "" && createdate != "") {
        $.ajax({
          url: "/emp/student/modStudent",
          type: "POST",
          dataType: "json",
          data: {
            id: getQueryVariable("id"),
            no: no,
            name: name,
            sex: sex,
            cardno: cardno,
            birthday: birthday,
            school: school,
            education: education,
            class_id: class_id,
            email: email,
            qq: qq,
            phone: phone,
            createdate: createdate
          }, //提交表单的数据
          success: function(res) {
            window.location.reload();
          },
          error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest.status);
            alert(XMLHttpRequest.readyState);
            alert(textStatus);
          }
        });
      }else{
        alert("请完善学生信息！");
      }
    }
	</script>
</body>
</html>